* The analysis was conducted using Stata 16. 
clear
* Change directory to where the folder with the data is
cd "Data Archive TWT Predictions\DSBC"
use DatasetDSBC, clear

*Need to repeat all analyses for the four conditions in this dataset separately (Time Pressure vs. No Pressure, Pie vs. Bar Lottery representation)
*The conditions are identified by the variable Pie = 0 or 1 and nopressure = 0 or 1.
******************
keep if nopressure==1
keep if Pie==0
*******************

*Calculate choice frequency
egen sumleft= sum(choiceleft), by(subj idpair)
tab sumleft
gen choiceright = 1-choiceleft
egen sumright= sum(choiceright), by(subj idpair)
tab sumright

*Transform RTs conditional on choices
gen logrtA = log(rt) if choiceleft==1
gen logrtB = log(rt) if choiceleft==0
*create a duplicate of the RTs variables to be used as grid points for the estimation of the CDFs
gen RTordered = rt
gen logRTordered = log(RTordered)

*Deterministic choices are always revealed
gen deterministic=0
replace deterministic =1 if sumleft==0 | sumleft==12

*sort the dataset based on all response times, this gives the grid of points for which the CDF of RTs will be calculated.
sort RTordered, stable
save baseNopressureBar, replace 
*Adapt the program for the other 4 conditions.
*Files should be called:
*	baseTimepressureBar
*	baseTimepressurePie
*	baseNopressurePie

use baseNopressureBar, clear
*Calculate the CDF of RTs by looping over subjects and choice pairs. Use a non-adaptive kernel with epanechnikov specification and bandwidth of 0.01 as in Alos-Ferrer, Fehr, and Netzer (2021).
*Because of how akdensity is implemented, save the estimated CDFs conditional on the choice (left vs. right) separately and then merge the two datasets back.	

 local j = 1
          while `j' < 61{
 local i = 1
          while `i' < 21{
          gen rtAs`j'id`i' = logrtA if (sumleft>1 & idpair==`i'  & subject==`j')
    sum rtAs`j'id`i'
    if r(N)!=0{
    di as result "idpair `i'" as error "subject`j'"
akdensity rtAs`j'id`i'   , nograph  noadaptive   at(logRTordered) cdf(cdfAsubj`j'id`i') 
          }
		  drop rtAs`j'id`i'
local i = `i' + 1
            }   
local j = `j' + 1
            }	
					
*Density estimation with only one point					
 local j = 1
          while `j' < 61{
 local i = 1
          while `i' < 21{
          gen rtAs`j'id`i' = logrtA if (sumleft==1  & idpair==`i'  & subject==`j')
    sum rtAs`j'id`i'
    if r(N)!=0{
    di as result "idpair `i'" as error "subject`j'"
akdensity rtAs`j'id`i'   , nograph kernel(epanechnikov) noadaptive bwidth(0.01)  at(logRTordered) cdf(cdfAsubj`j'id`i') 
          }
		  drop rtAs`j'id`i'
local i = `i' + 1
            }    
local j = `j' + 1
            }					
 save LeftChoiceNopressureBars, replace
 
 *Repeat CDF estimation for the other choice
use baseNopressureBar,clear
 local j = 1
          while `j' < 61{
 local i = 1
          while `i' < 21{
          gen rtBs`j'id`i' = logrtB if (sumright>1 & idpair==`i'  & subject==`j')
    sum rtBs`j'id`i'
    if r(N)!=0{
    di as result "idpair `i'" as error "subject`j'"
akdensity rtBs`j'id`i'   , nograph kernel(epanechnikov) noadaptive   at(logRTordered) cdf(cdfBsubj`j'id`i') 
          }
		  drop rtBs`j'id`i'
local i = `i' + 1
            }   
local j = `j' + 1
            }	
						
 local j = 1
          while `j' < 61{
 local i = 1
          while `i' < 21{
          gen rtBs`j'id`i' = logrtB if (sumright==1  & idpair==`i'  & subject==`j')
    sum rtBs`j'id`i'
    if r(N)!=0{
    di as result "idpair `i'" as error "subject`j'"
akdensity rtBs`j'id`i'   , nograph kernel(epanechnikov) noadaptive bwidth(0.01)  at(logRTordered) cdf(cdfBsubj`j'id`i') 
          }
		  drop rtBs`j'id`i'
local i = `i' + 1
            }    
local j = `j' + 1
            }					
 save RightChoiceNopressureBars, replace

*Merge the CDFs estimations
use baseNopressureBar,clear 
sort RTordered
merge 1:1 subj trial using LeftChoiceNopressureBars, nogenerate
merge 1:1 subj trial using RightChoiceNopressureBars, nogenerate

*Drop trials where choices were not recorded
keep if choiceleft !=.

*Count repetitions for each subject and pair
egen repetitions = count(idpair), by(subj idpair)
egen maxrep = max(Counting), by(subj idpair)

*Calculate ratios
gen Ratioleft = sumleft/repetition
gen Ratioright = sumright/repetition
tab Ratioleft Ratioright
 
*Check if the conditions identified by Theorem 1 (Alos-Ferrer, Fehr, and Netzer, 2021, Theorem 2) are fulfilled.
*Loop over subjects and pairs 
gen revpref=.
sort RTordered
quietly{
local j = 1
          while `j' < 61{
local i = 1
         while `i' < 21{
	capture confirm variable cdfAsubj`j'id`i'
        if !_rc {
	capture confirm variable cdfBsubj`j'id`i'
        if !_rc {		
	 gen ratioAsubj`j'id`i' = Ratioleft  if  idpair==`i' & subj==`j'
	 gen ratioBsubj`j'id`i' = Ratioright	 if  idpair==`i' & subj==`j' 
gen pfpref  = ratioAsubj`j'id`i'  * cdfAsubj`j'id`i'  
gen pfnotpref  = ratioBsubj`j'id`i'  * cdfBsubj`j'id`i' 
gen diff  =  pfpref - pfnotpref 
sum diff, detail
egen qfsdh1=max(sign(diff))
egen qfsdh2=min(sign(diff))
gen qfsd`j'`i'=qfsdh1-qfsdh2
replace qfsd`j'`i'=1 if qfsd`j'`i'==0
replace qfsd`j'`i'=0 if qfsd`j'`i'==2

gen revpref`j'id`i'=0 if qfsd`j'`i'==0
replace revpref`j'id`i'=1 if qfsd`j'`i'==1 & Ratioleft >0.5
replace revpref`j'id`i'=-1 if qfsd`j'`i'==1 & Ratioleft <0.5
replace revpref`j'id`i'=1 if qfsd`j'`i'==1 & Ratioleft ==0.5 & diff>0
replace revpref`j'id`i'=-1 if qfsd`j'`i'==1 & Ratioleft ==0.5 & diff<0
replace revpref =revpref`j'id`i'  if  idpair==`i' & subj==`j'

drop ratioAsubj`j'id`i' ratioBsubj`j'id`i' qfsd`j'`i' revpref`j'id`i' diff pfpref pfnotpref qfsdh1  qfsdh2
}
}
local i = `i' + 1
            }
local j = `j' + 1
            }
}

tab revpref
*deterministic choices are always revealed			
replace revpref =1 if Ratioleft==1
replace revpref =-1 if Ratioright==1
tab revpref, missing

save revealedprefDSBCNoTimePressureBars, replace
*Adapt the program for the other 4 conditions.
*Files should be called:
*	revealedprefDSBCNoTimePressurePie
*	revealedprefDSBCTimePressureBars
*	revealedprefDSBCTimePressurePie

